Next up on the agenda is an XNA program that displays
a little greeting in the center of the screen. While text is often
prevalent in Silverlight applications, it is less common in graphical
games. In games, text is usually relegated to describing how the game
works or displaying the score, so the very concept of a “hello, world” program doesn’t quite fit in with the whole XNA programming paradigm.
In fact, XNA doesn’t even have any built-in fonts. You might think that an XNA program
running on the phone can make use of the same native fonts as
Silverlight programs, but this is not so. Silverlight uses vector-based
TrueType fonts and XNA doesn’t know anything about such exotic concepts.
To XNA, everything is a bitmap, including fonts.
If you wish to use a particular font in your XNA program, that font must be embedded into the executable as a collection of bitmaps for each character. XNA
Game Studio (which is integrated into Visual Studio) makes the actual
process of font embedding very easy, but it raises some thorny legal
issues. You can’t legally distribute an XNA program unless you can also
legally distribute the embedded font, and with most of the fonts
distributed with Windows itself or Windows applications, this is not the
case.
To help you out of this legal quandary, Microsoft licensed some fonts from Ascender Corporation specifically for the purpose of allowing you to embed them in your XNA programs. Here they are:
Notice that the Pericles font uses small capitals for lower-case letters, so it’s probably suitable only for headings.
From the File menu of Visual
Studio select New and Project. On the left of the dialog box, select
Visual C# and XNA Game Studio 4.0. In the middle, select Windows Phone
Game (4.0). Select a location and enter a project name of XnaHelloPhone.
Visual Studio creates two projects, one for the program and the other for the program’s content.
XNA programs usually contain lots of content, mostly bitmaps and 3D
models, but fonts as well. To add a font to this program, right-click
the Content project (labeled “XnaHelloPhoneContent (Content)” and from
the pop-up menu choose Add and New Item. Choose Sprite Font, leave the
filename as SpriteFont1.spritefont, and click Add.
The word “sprite” is common in
game programming and usually refers to a small bitmap that can be moved
very quickly, much like the sprites you might encounter in an enchanted forest. In XNA, even fonts are sprites.
You’ll see
SpriteFont1.spritefont show up in the file list of the Content
directory, and you can edit an extensively commented XML file describing
the font.
Example 1. XNA Project: XnaHelloPhone File: SpriteFont1.spritefont (complete w/o comments)
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics"> <Asset Type="Graphics:FontDescription"> <FontName>Segoe UI Mono</FontName> <Size>14</Size> <Spacing>0</Spacing> <UseKerning>true</UseKerning> <Style>Regular</Style> <CharacterRegions> <CharacterRegion> <Start> </Start> <End>~</End> </CharacterRegion> </CharacterRegions> </Asset> </XnaContent>
|
Within the FontName tags you’ll see Segoe UI Mono, but you can change that to one of the other fonts
I listed earlier. If you want Pericles Light, put the whole name in
there, but if you want Miramonte Bold or Pescadero Bold or Segoe UI Mono
Bold, use just Miramonte or Pescadero or Segoe UI Mono, and enter the
word Bold between the Style tags. You can use Bold for the other fonts
as well, but for the other fonts, bold will be synthesized, while for
Miramonte or Pescadero or Segoe UI Mono, you’ll get the font actually
designed for bold.
The Size
tags indicate the point size of the font. In XNA as in Silverlight, you
deal almost exclusively with pixel coordinates and dimensions, but the
conversion between points and pixels used within XNA is based on 96 DPI
displays. The point size of 14 becomes a pixel size of 18-2/3 within
your XNA program. This is very close to the 15-point and 20-pixel “normal” FontSize in Silverlight for Windows Phone.
The CharacterRegions
section of the file indicates the ranges of hexadecimal Unicode
character encodings you need. The default setting from 0x32 through
0x126 includes all the non-control characters of the ASCII character
set.
The filename of
SpriteFont1.spritefont is not very descriptive. I like to rename it to
something that describes the actual font; if you’re sticking with the
default font settings, you can rename it to Segoe14.spritefont. If you
look at the properties for this file—right-click the filename and select
Properties—you’ll see an Asset Name that is also the filename without
the extension: Segoe14. This Asset Name is what you use to refer to the
font in your program to load the font. If you want to confuse yourself,
you can change the Asset Name independently of the filename.
In its initial state, the XNAHelloPhone project contains two C# code files: Program.cs and Game1.cs.
The first is very simple and turns out to be irrelevant for Windows
Phone 7 games! A preprocessor directive enables the Program
class only if a symbol of WINDOWS or XBOX is defined. When compiling
Windows Phone programs, the symbol WINDOWS_PHONE is defined instead.